<template>
  <div class="container">
    <div class="top">
      <el-input style="width:200px;margin-bottom:5px" v-model="search.searchName" clearable
        placeholder="请输入客户名称或编码"></el-input>
      <el-button size="small" type="primary" class="handleSearch" icon="el-icon-search"
        @click="handleSearch()">查询</el-button>
      <el-button size="small" type="primary" class="handleSearch" icon="el-icon-circle-plus-outline"
        @click="addUser()">客户添加</el-button>
      <el-button class="add" size="small" type="success" @click="addPermission" icon="el-icon-refresh">全部同步</el-button>
    </div>
    <el-table max-height="600" :data="tableData" border style="width: 100%" empty-text="暂无数据">

      <el-table-column prop="name" label="客户名称" width="180" align="center"></el-table-column>
      <el-table-column prop="erp_org_code" label="店铺编号" width="180" align="center"></el-table-column>
      <el-table-column prop="org_area_code" label="区域编码" width="180" align="center"></el-table-column>
      <el-table-column prop="org_area_name" label="区域名称" width="180" align="center"></el-table-column>
      <el-table-column prop="address" label="地址" align="center"></el-table-column>
      <el-table-column prop="typeStr" label="有效状态" width="80" align="center"></el-table-column>
      <el-table-column prop="create_at" label="添加时间" align="center"></el-table-column>
      <el-table-column prop="update_at" label="最后同步时间" align="center"></el-table-column>
      <el-table-column label="门店状态设置" align="center" width="180">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.is_openMsg" active-text="开启" inactive-text="闭店"
            @change="is_open(scope)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope)"
            v-if="scope.row.type == 2">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope)"
            v-if="scope.row.type == 2">删除</el-button>
          <el-button type="success" size="mini" icon="el-icon-refresh" v-if="scope.row.type == 1"
            @click="handleEdits(scope)">同步</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
    <storeDialog :dialog="dialog" @upData="getData"></storeDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { storeList, synchronization, delorg, optOpenStatusOrg } from "@/api/store/storeList";
import StoreDialog from "@/components/store/storeDialog";

export default {
  components: {
    pagination: Pagination,
    storeDialog: StoreDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: ""
      },
      search: {
        searchName: "",
        searchCode: ""
      }
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        key: this.search.searchName
      };
      storeList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              res.typeStr = res.is_enable == 1 ? "有效" : "无效";
              res.is_openMsg = res.is_open == 1 ? true : false;
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //同步
    handleEdits(scope) {
      this.$confirm("是否同步?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let data = {
          code: scope.row.erp_org_code
        };
        synchronization(data).then(res => {
          if (res.code == 0) {
            this.getData();
            this.$message({
              type: "success",
              message: res.msg
            });
          }
        });
      });
    },
    //同步所有
    addPermission() {
      this.$confirm("是否全部同步?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        synchronization().then(res => {
          if (res.code == 0) {
            this.getData();
            this.$message({
              type: "success",
              message: res.msg
            });
          }
        });
      });
    },
    //添加客户
    addUser() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
      this.dialog.id = "";
    },
    // 编辑
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.id = scope.row.id;
    },
    //删除
    handleDelete(scope) {
      let data = {
        id: scope.row.id
      }
      this.$confirm("是否删除该客户?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delorg(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg || res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //门店状态设置
    is_open(scope) {

      let data = {
        id: scope.row.id,
        is_open: scope.row.is_openMsg == true ? 1 : 0,
      };
      optOpenStatusOrg(data).then((res) => {
        if (res.code == 0) {
          this.getData();
          this.$message({
            type: "success",
            message: res.msg,
          });
        }
      });
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },

  }
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}

.pagination {
  text-align: right;
  padding: 10px;
}
</style>
